package ferp.core.ai.filter;

import ferp.core.card.Card;
import ferp.core.game.Trick;
import ferp.core.player.Hand;

/**
 * User: olegs
 * Date: 1/21/13 6:48 PM
 */
public class FilterPlayerNonFirstHandMove
{
  // we should get here only if the player is not a first hand
  public static int apply(int candidates, int player, int trick, Card.Suit trump, int hand)
  {
    // remember current highest card in the trick
    Card highest = Trick.highest(trick, trump);
    int filtered = 0;

    for (Card.Suit s : Card.Suit.rising)
    {
      int set = Card.Set.get(candidates, s);

      if (set != 0)
      {
        // simulate player move with the highest card in this suit
        trick = Trick.add(trick, player, Card.card(s, Card.Set.highest(set)).compressed);
        // get new highest card in the trick
        Card newHighest = Trick.highest(trick, trump);

        // if the highest card has been changed return all candidates as a player might win the trick
        if (highest != newHighest)
          return candidates;

        // the highest card has not been changed - add only the lowest card of this suit as candidate
        filtered = Hand.add(filtered, s, Card.Set.lowest(set));

        // restore trick
        trick = Trick.pop(trick);
      }
    }

    if (filtered == 0)
      return candidates;

    return filtered;
  }
}
